package file;

import org.json.JSONException;
import org.json.JSONObject;
import utils.FileUtils;
import utils.TextUtils;

import java.io.File;
import java.util.HashMap;

/**
 * Created by CatM on 18/1/31.
 */
public class FileManager {
    private static FileManager INSTANCE = new FileManager();
    private JSONObject notifyJson;
    // "../../file/notify.json"
    private File notifyFile = new File("");
    private boolean mLoaded = false;

    private FileManager() {
        mLoaded = false;
        loadAll();
    }

    public static FileManager get() {
        return INSTANCE;
    }

    private void loadAll() {
        synchronized (this) {
            mLoaded = false;
        }
        notifyJson = new JSONObject();
        mLoaded = true;
//        String path = new File("").getAbsolutePath()+"/file/notify.json";
//        notifyFile = new File(path);
//        System.out.print("path=="+path);
//        new Thread("File-Load") {
//            @Override
//            public void run() {
//                super.run();
//                synchronized (FileManager.this) {
//                    String notifyStr = FileUtils.readBytesToString(notifyFile);
//                    mLoaded = true;
//                    try {
//
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//                    FileManager.this.notifyAll();
//                }
//            }
//        }.start();
    }

    public String getString(String key) throws JSONException {
        synchronized (this) {
            try {
                awaitLoadedLocked();
                if (TextUtils.notEmpty(key) && notifyJson != null && notifyJson.has(key)) {
                    return notifyJson.getString(key);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return "";
        }
    }

    public int getInt(String key) {
        synchronized (this) {
            try {
                awaitLoadedLocked();
                if (TextUtils.notEmpty(key) && notifyJson != null && notifyJson.has(key)) {
                    return notifyJson.getInt(key);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return -1;
        }
    }

    private void awaitLoadedLocked() {
        while (!mLoaded) {
            try {
                wait();
            } catch (InterruptedException unused) {
            }
        }
    }

    public Editor edit() {
        synchronized (this) {
            awaitLoadedLocked();
        }
        return new Editor();
    }

    public class Editor {

        public Editor putString(String key, String value) {
            try {
                synchronized (this) {
                    if (TextUtils.notEmpty(key) && notifyJson != null) {
                        notifyJson.put(key, value);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return this;
        }

        public Editor putInt(String key, int value) {
            try {
                synchronized (this) {
                    if (TextUtils.notEmpty(key) && notifyJson != null) {
                        notifyJson.put(key, value);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return this;
        }

        public void commit() {
//            synchronized (this) {
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        FileUtils.writeString(notifyFile, notifyJson.toString());
//                    }
//                }).start();
//            }
        }
    }

    public static void main(String[] args) {
        FileManager.get().edit().putInt("https://m.bacaoo.com/p/70777", 14).commit();
        int count = FileManager.get().getInt("https://m.bacaoo.com/p/70777");
        System.out.println("Count="+count);
    }

}
